 function setCert(ssl)
             local crt = assert(io.open("/Users/elbert/Documents/openresty-test/conf/server.crt"))
             local cert_data = crt:read("*a")
             crt:close()
             local cert, err = ssl.parse_pem_cert(cert_data)
              if not cert then
                  ngx.log(ngx.ERR, "failed to parse PEM cert: ", err)
                  return
              end
             local ok, err = ssl.set_cert(cert)
             if not ok then
                 ngx.log(ngx.ERR, "failed to set  cert: ", err)
                 return
             end
             ngx.log(ngx.ERR, "sucess to set  cert!")

end

function setKey(ssl)
              local key = assert(io.open("/Users/elbert/Documents/openresty-test/conf/server.key"))
              local key_data = key:read("*a")
              key:close()
              local pkey, err = ssl.parse_pem_priv_key(key_data)
              if not pkey then
               ngx.log(ngx.ERR, "failed to parse pem key: ", err)
               return
               end

               local ok, err = ssl.set_priv_key(pkey)
               if not ok then
                    ngx.log(ngx.ERR, "failed to set private key: ", err)
                    return
               end
               ngx.log(ngx.ERR, "sucess to set key!")

end

function clearCert(ssl)             
             -- 清除之前设置的证书和私钥
             local ok, err = ssl.clear_certs()
             if not ok then
               ngx.log(ngx.ERR, "failed to clear existing (fallback) certificates")
                return ngx.exit(ngx.ERROR)
             end
end 


local ssl = require "ngx.ssl"
clearCert(ssl)
setCert(ssl)
setKey(ssl)
